package org.cytoscape.myapp.cytoscape_app_v1.internal.tasks.visualHideEdge;

import java.util.List;

import org.cytoscape.application.CyApplicationManager;
import org.cytoscape.model.CyEdge;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNode;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.work.Task;
import org.cytoscape.work.TaskMonitor;

public class CreateVirtualEdgeTask implements Task{

	private CyNetwork network;
	private CyNetworkView networkView;
	private StateVisualEdge stateEdge;
	
	
	
	public CreateVirtualEdgeTask(CyApplicationManager applicationManager, StateVisualEdge stateEdge ){
		this.network=applicationManager.getCurrentNetwork();
		this.networkView = applicationManager.getCurrentNetworkView();
		this.stateEdge = stateEdge;
	}
	
	public void run(TaskMonitor taskMonitor) throws Exception {
		taskMonitor.setTitle("Create virtual Edge");
		taskMonitor.setStatusMessage("Iniciando");
		
		if (network.getDefaultEdgeTable().getColumn("Visible") == null)
			network.getDefaultEdgeTable().createColumn("Visible", Boolean.class, false);
		List<CyNode> listNodes = network.getNodeList();
		network.getDefaultEdgeTable().setPublic(false);
		for(int i = 0;i<listNodes.size();i++){
			for(int j = i;j<listNodes.size();j++){
				if(!listNodes.get(i).equals(listNodes.get(j))){
						if ((!network.containsEdge(listNodes.get(i), listNodes.get(j)))&&(!network.containsEdge(listNodes.get(j), listNodes.get(i)))){
							CyEdge newEdge =network.addEdge(listNodes.get(i), listNodes.get(j), false);
							String nodo1 = network.getDefaultNodeTable().getRow(listNodes.get(i).getSUID()).get("shared name", String.class);
							String nodo2 = network.getDefaultNodeTable().getRow(listNodes.get(j).getSUID()).get("shared name", String.class);
							network.getDefaultEdgeTable().getRow(newEdge.getSUID()).set("shared name",nodo1+" (Amigode) "+nodo2);
							network.getDefaultEdgeTable().getRow(newEdge.getSUID()).set("name",nodo1+" (Amigode) "+nodo2);
							network.getDefaultEdgeTable().getRow(newEdge.getSUID()).set("Visible", !stateEdge.getStateVisibleEdge());
						}
					}
			}
		}
		taskMonitor.setTitle("Finalizado");
	}

	@Override
	public void cancel() {
		// TODO Auto-generated method stub
		
	}
	
	
}
